package com.changgou.goods.controller;

import com.changgou.goods.pojo.Album;
import com.changgou.goods.service.AlbumService;
import com.github.pagehelper.PageInfo;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author seafwg
 * @Date 2021/11/25 12:34 上午
 * @Version 1.0
 * @description xxx
 */
@RestController
@CrossOrigin
@RequestMapping(value = "/album")
public class AlbumController {
    @Autowired
    private AlbumService albumService;

    /**
     * 新增相册
     * @param album
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Album album) {
        albumService.add(album);
        return new Result(true, StatusCode.OK, "相册添加成功！");
    }

    /**
     * 删除相册：
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete/{id}")
    public Result delete(@PathVariable Long id) {
        albumService.delete(id);
        return new Result(true, StatusCode.OK, "删除相册成功！");
    }

    /**
     * 更新相册：
     * @param album
     * @param id
     * @return
     */
    @PutMapping(value = "/update/{id}")
    public Result update(@RequestBody Album album, @PathVariable Long id) {
        //设置转：
        album.setId(id);
        //修改数据
        albumService.update(album);
        return new Result(true, StatusCode.OK, "更新相册成功！");
    }

    /**
     * 根据id查找相册
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}")
    public Result<Album> findById(@PathVariable Long id) {
        Album album = albumService.findById(id);
        return new Result<Album>(true, StatusCode.OK, "查找成功！", album);
    }

    /**
     * 查询所有相册：
     * @return
     */
    @GetMapping(value = "/all")
    public Result<Album> findAll() {
        List<Album> albums = albumService.findAll();
        return new Result<Album>(true, StatusCode.OK, "查询成功!", albums);
    }

    /**
     * 多条件查询：
     * @param album
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List<Album>> findList(@RequestBody(required = false) Album album) {
        List<Album> albumList = albumService.findList(album);
        return new Result<List<Album>>(true, StatusCode.OK, "多条件查询成功！", albumList);
    }

    /**
     * 分页查询：
     * @param page 当前页
     * @param size 每页大小
     * @return
     */
    @GetMapping (value = "/search/{page}/{size}")
    public Result<List<Album>> findPage(@PathVariable Integer page, @PathVariable Integer size) {
        PageInfo<Album> pageInfo = albumService.findPage(page, size);
        return new Result<List<Album>>(true, StatusCode.OK, "分页查询成功！", pageInfo);
    }

    /**
     * 多条件分页查询：
     * @param album
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}")
    public Result<List<Album>> findPage(@RequestBody(required = false) Album album, @PathVariable Integer page, @PathVariable Integer size) {
        PageInfo<Album> pageInfo = albumService.findPage(album, page, size);
        return new Result<List<Album>>(true, StatusCode.OK, "多条件分页查询成功！", pageInfo);
    }


}
